package com.le.tester.book.effectivejava3rdchinese.articleeightyone;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * createTime：2022/10/10 14:10
 * description：
 */
public class Demo {

    private static final ConcurrentMap<String, String> map = new ConcurrentHashMap<>();

    public static String intern(String s) {
        //如果存在key,则根据key来获取到value，返回value
        String previousVal = map.putIfAbsent(s, s);
        return previousVal == null ? s : previousVal;
    }

    public static String internPlus(String s) {
        String result = map.get(s);
        if (null == result) {
            result = map.putIfAbsent(s, s);
            if (null == result) {
                result = s;
            }
        }
        return result;
    }

}
